package com.leetcode.algorithm.y22.m06;

/**
 * 977. 有序数组的平方
 * 
 * https://leetcode.cn/problems/squares-of-a-sorted-array/
 * 
 * @author jie.deng
 *
 */
class Question0977Solution01 {

	public int[] sortedSquares(int[] nums) {
		int len = nums.length;
		int[] ret = new int[len];
		int retIdx = len - 1;
		if (nums.length == 0) {
			return ret;
		}
		int left = 0;
		int right = len - 1;
		while (left <= right) {
			int lowPow = nums[left] * nums[left];
			int highPow = nums[right] * nums[right];
			if (lowPow > highPow) {
				ret[retIdx] = lowPow;
				left++;
			} else {
				ret[retIdx] = highPow;
				right--;
			}
			retIdx--;
		}
		return ret;
	}

}